export default {
	state:{
		cartData:JSON.parse(uni.getStorageSync('catlist') || '[]'),
		cartAddress:JSON.parse(uni.getStorageSync('addosobjt') || '{}'),
	},
	mutations:{
		//把商品信息存到本地
		addgetbenti(state){
			uni.setStorageSync('catlist', JSON.stringify(state.cartData))
		},
		//当点击加入购物车的时候把该商品信息加入到数组中
		addToCart(state,goods){
			let arr = state.cartData.some(item=>{
				return goods.goods_id == item.goods_id
			})
			if(arr){
				state.cartData.map(item=>{
					if(item.goods_id === goods.goods_id){
						item.goods_count ++
					}
				})
				uni.$showMsg('该商品已存在购物车里，已默认为你数量+1啦~')
			}else{
				state.cartData.push(goods)
			}
			this.commit('addgetbenti')
		},
		//更改选中状态
		addxuanzh(state,itemm){
			const xuanzo = state.cartData.find(item=>item.goods_id == itemm.goods_id)
			xuanzo.goods_state = itemm.goods_state
			this.commit('addgetbenti')
		},
		//商品数量变化
		addnumbo(state,itemm){
			const nubom = state.cartData.find(item=>item.goods_id == itemm.goods_id)
			nubom.goods_count = itemm.goods_count
			this.commit('addgetbenti')
		},
		//商品删除
		addgetlistdiet(state,index){
			state.cartData.splice(index,1)
			this.commit('addgetbenti')
		},
		//收货地址放本地
		addcundizhi(state){
			uni.setStorageSync('addosobjt',JSON.stringify(state.cartAddress))
		},
		//存放收货地址信息
		addcartAdds(state,objtct){
			state.cartAddress = objtct
			this.commit('addcundizhi')
		},
		//全选点击状态变化
		zhuangtbh(state,sum){
			if(sum){
				state.cartData.map(item=>{
					item.goods_state = false
				})
			}else{
				state.cartData.map(item=>{
					item.goods_state = true
				})
			}
			this.commit('addgetbenti')
		}
	},
	getters:{
		total(state){
			let c = 0
			state.cartData.map(item=>{
				c += item.goods_count
			})
			return c
		},
		//选中的商品总价
		prlce(state){
			let arr = 0
			state.cartData.map(item=>{
				if(item.goods_state){
					arr += item.goods_price * item.goods_count
				}
			})
			return arr
		},
		//选中的商品数量
		sumo(state){
			let som = 0
			state.cartData.map(item=>{
				if(item.goods_state){
					som += item.goods_count
				}
			})
			return som
		},
		//底部结算栏全选状态变化
		state(state){
			return state.cartData.every(item=>item.goods_state)
		}
	}
}